import { ref, reactive } from 'vue'
import { KnowledgeCreate, KnowledgeFileHandle } from '~/api/manager'
import { showModel, toast } from '~/composables/util'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'


export function knowledgeCreate() {
    const router = useRouter()
    const formDrawerRef = ref(null)
    const formRef = ref(null)
    const loading = ref(false)
    const form = reactive({
        knowledge_name: '',
        description: '',
        embedding_server_id: ''
    })
  
    const rules = {
        knowledge_name: [
        { required: true, message: '知识库名称不能为空', trigger: 'blur' },
      ],
      embedding_server_id: [
        { required: true, message: '向量算法不能为空', trigger: 'blur' }
      ]
    }
  
    const onSubmit = () => {
      formRef.value.validate((valid) => {
        if (!valid) {
          return false
        }
        loading.value = true
        KnowledgeCreate(form)
          .then((res) => {
            if (res.code === 0) {
              toast('创建成功')
              formDrawerRef.value.close()
              const knowledge_id = res.data
              router.push(`/knowledge/${knowledge_id}`)
  
            } else {
              toast(res.message, 'error')
            }
          })
          .finally(() => {
            loading.value = false
          })
      })
    }
  
    const openRegisterForm = () => formDrawerRef.value.open()
  
    return {
      formDrawerRef,
      formRef,
      form,
      onSubmit,
      rules,
      openRegisterForm,
      loading
    }
  }


  export function KnowledgeFileHandleManger() {
    const router = useRouter()
    const formDrawerRef = ref(null)
    const formRef = ref(null)
    const loading = ref(false)
    const form = reactive({
      knowledge_id: '',
      file_id: '',
      split_size: 0,
      split_str: '',
      file_extract_id: '',
    })
  
    const rules = {
      split_size: [
        { required: true, message: '分词大小不能为空', trigger: 'blur' },
        { type: 'number', min: 30, max: 4096, message: '分词大小应在 30 到 4096 个字符之间', trigger: 'blur' }
      ],
      split_str: [
        { required: true, message: '分词关键词不能为空', trigger: 'blur' }
      ],
      file_extract_id: [
        { required: true, message: '文本提取方法不能为空', trigger: 'blur' }
      ]
    }
  
    const onSubmit = () => {
      formRef.value.validate((valid) => {
        if (!valid) {
          return false
        }
        loading.value = true
        console.log('KnowledgeFileHandleManger', form)
        KnowledgeFileHandle(form)
          .then((res) => {
            if (res.code === 0) {
              toast('开始处理')
              formDrawerRef.value.close()
  
            } else {
              toast(res.message, 'error')
            }
          })
          .finally(() => {
            loading.value = false
          })
      })
    }
  
    const openRegisterForm = () => formDrawerRef.value.open()
  
    return {
      formDrawerRef,
      formRef,
      form,
      onSubmit,
      rules,
      openRegisterForm,
      loading
    }
  }



